from typing import Annotated

import uvicorn
from fastapi import FastAPI, Path, Query
from fastapi.openapi.docs import get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html, get_redoc_html
from starlette.staticfiles import StaticFiles
from pydantic import BaseModel

# 创建FastAPI实例，禁用掉FastAPI会自动生成Swagger UI和Redoc文档
app = FastAPI(docs_url=None, redoc_url=None)

# 静态文件挂载（路由与静态文件映射）
app.mount("/static", StaticFiles(directory="../statics"), name="static")


# 重新提供一个Swagger UI，相当于我们自己重写了这么一个路由
@app.get("/docs", include_in_schema=False)
def custom_swagger_ui():
    return get_swagger_ui_html(
        openapi_url = "/openapi.json",
        title = "Swagger UI for FastAPI",
        swagger_js_url="/static/swagger-ui-bundle.min.js",
        swagger_css_url="/static/swagger-ui.min.css"
    )
@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
    return get_swagger_ui_oauth2_redirect_html()

# 这里与Swagger UI一样，重新提供Redoc
@app.get("/redoc", include_in_schema=False)
def custom_redoc():
    return get_redoc_html(
        openapi_url = "/openapi.json",
        title = "Redoc for FastAPI",
        redoc_js_url="/static/redoc.standalone.js"
    )

@app.get("/items/{item_id}")
async def read_item(
        item_id: Annotated[int, Path(title="这是一个路径参数id")]
):
    return {"item_id": item_id}

# @app.get("/orders/{order_id}")
# async def read_order(
#         order_id: Annotated[str, Path(title="这是一个路径参数id")],
#         order_no: str
# ):
#     return {"order_id": order_id, "order_no": order_no}

# @app.get("/orders/{order_id}")
# async def read_order(
#         order_no: str,
#         order_id: int = Path(title="这是一个路径参数id")
#
# ):
#     return {"order_id": order_id, "order_no": order_no}

@app.get("/orders/{order_id}")
async def read_order(
        *,
        order_id: int = Path(title="这是一个路径参数id"),
        order_no: str

):
    return {"order_id": order_id, "order_no": order_no}

@app.get("/user/{user_id}")
async def read_user(user_id: Annotated[int, Path(gt=0,le=1000)],user_name: str):
    return {"user_id": user_id, "user_name": user_name}

@app.get("/goods/{goods_id}")
async def read_goods(goods_id: Annotated[int, Path(gt=0,le=1000)],size: Annotated[float, Query(gt=0,lt=42.5)]):
    return {"goods_id": goods_id, "size": size}


if __name__ == '__main__':
    app.debug = True
    uvicorn.run("main:app", host="127.0.0.1", port=8005, reload=True)

